// pages/my/my.js
var httpTool = require('../..//utils/network.js');
var util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    text: '',
    imageArray: [],
    lat: '',
    lng: '',
    coordinateName: '所在位置',
  },
  onLoad: function(options) {
    // 生命周期函数--监听页面加载
  },
  //1.动态文字
  textListen: function(event) {
    var str = event.detail.value
    console.log(str)
    this.setData({
      text: str
    })
  },
  //2.相册

  selectImageClick: function(event) {
    console.log('图片')
    var that = this
    var count = 9 - that.data.imageArray.length
    var i = 0
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths[i])
        wx.uploadFile({
          url: app.data.httpUrl + 'v1/upload/res',
          filePath: tempFilePaths[i],
          name: 'file',
          success: function(res) {
            var obj = JSON.parse(res.data)
            console.log(obj)
            if (obj.code == 200) {
              var model = {
                logoId: obj.entity.id,
                logoUrl: obj.entity.fileUrl
              }
              that.data.imageArray.push(model)
              that.setData({
                imageArray: that.data.imageArray,
              })
              i++;
              if (i == count) {
                return;
              }
              wx.uploadFile({
                url: app.data.httpUrl + 'v1/upload/res',
                filePath: tempFilePaths[i],
                name: 'file',
                success: function(res) {
                  var obj = JSON.parse(res.data)
                  console.log(obj)
                  if (obj.code == 200) {
                    var model = {
                      logoId: obj.entity.id,
                      logoUrl: obj.entity.fileUrl
                    }
                    that.data.imageArray.push(model)
                    that.setData({
                      imageArray: that.data.imageArray,
                    })
                    i++;
                    if (i == count) {
                      return;
                    }
                    wx.uploadFile({
                      url: app.data.httpUrl + 'v1/upload/res',
                      filePath: tempFilePaths[i],
                      name: 'file',
                      success: function(res) {
                        var obj = JSON.parse(res.data)
                        console.log(obj)
                        if (obj.code == 200) {
                          var model = {
                            logoId: obj.entity.id,
                            logoUrl: obj.entity.fileUrl
                          }
                          that.data.imageArray.push(model)
                          that.setData({
                            imageArray: that.data.imageArray,
                          })
                          i++;
                          if (i == count) {
                            return;
                          }
                          wx.uploadFile({
                            url: app.data.httpUrl + 'v1/upload/res',
                            filePath: tempFilePaths[i],
                            name: 'file',
                            success: function(res) {
                              var obj = JSON.parse(res.data)
                              console.log(obj)
                              if (obj.code == 200) {
                                var model = {
                                  logoId: obj.entity.id,
                                  logoUrl: obj.entity.fileUrl
                                }
                                that.data.imageArray.push(model)
                                that.setData({
                                  imageArray: that.data.imageArray,
                                })
                                i++;
                                if (i == count) {
                                  return;
                                }
                                wx.uploadFile({
                                  url: app.data.httpUrl + 'v1/upload/res',
                                  filePath: tempFilePaths[i],
                                  name: 'file',
                                  success: function(res) {
                                    var obj = JSON.parse(res.data)
                                    console.log(obj)
                                    if (obj.code == 200) {
                                      var model = {
                                        logoId: obj.entity.id,
                                        logoUrl: obj.entity.fileUrl
                                      }
                                      that.data.imageArray.push(model)
                                      that.setData({
                                        imageArray: that.data.imageArray,
                                      })
                                      i++;
                                      if (i == count) {
                                        return;
                                      }
                                      wx.uploadFile({
                                        url: app.data.httpUrl + 'v1/upload/res',
                                        filePath: tempFilePaths[i],
                                        name: 'file',
                                        success: function(res) {
                                          var obj = JSON.parse(res.data)
                                          console.log(obj)
                                          if (obj.code == 200) {
                                            var model = {
                                              logoId: obj.entity.id,
                                              logoUrl: obj.entity.fileUrl
                                            }
                                            that.data.imageArray.push(model)
                                            that.setData({
                                              imageArray: that.data.imageArray,
                                            })
                                            i++;
                                            if (i == count) {
                                              return;
                                            }
                                            wx.uploadFile({
                                              url: app.data.httpUrl + 'v1/upload/res',
                                              filePath: tempFilePaths[i],
                                              name: 'file',
                                              success: function(res) {
                                                var obj = JSON.parse(res.data)
                                                console.log(obj)
                                                if (obj.code == 200) {
                                                  var model = {
                                                    logoId: obj.entity.id,
                                                    logoUrl: obj.entity.fileUrl
                                                  }
                                                  that.data.imageArray.push(model)
                                                  that.setData({
                                                    imageArray: that.data.imageArray,
                                                  })
                                                  i++;
                                                  if (i == count) {
                                                    return;
                                                  }
                                                  wx.uploadFile({
                                                    url: app.data.httpUrl + 'v1/upload/res',
                                                    filePath: tempFilePaths[i],
                                                    name: 'file',
                                                    success: function(res) {
                                                      var obj = JSON.parse(res.data)
                                                      console.log(obj)
                                                      if (obj.code == 200) {
                                                        var model = {
                                                          logoId: obj.entity.id,
                                                          logoUrl: obj.entity.fileUrl
                                                        }
                                                        that.data.imageArray.push(model)
                                                        that.setData({
                                                          imageArray: that.data.imageArray,
                                                        })
                                                        i++;
                                                        if (i == count) {
                                                          return;
                                                        }
                                                        wx.uploadFile({
                                                          url: app.data.httpUrl + 'v1/upload/res',
                                                          filePath: tempFilePaths[i],
                                                          name: 'file',
                                                          success: function(res) {
                                                            var obj = JSON.parse(res.data)
                                                            console.log(obj)
                                                            if (obj.code == 200) {
                                                              var model = {
                                                                logoId: obj.entity.id,
                                                                logoUrl: obj.entity.fileUrl
                                                              }
                                                              that.data.imageArray.push(model)
                                                              that.setData({
                                                                imageArray: that.data.imageArray,
                                                              })
                                                              i++;
                                                              if (i == count) {
                                                                return;
                                                              }

                                                            }
                                                          }
                                                        })
                                                      }
                                                    }
                                                  })
                                                }
                                              }
                                            })
                                          }
                                        }
                                      })
                                    }
                                  }
                                })
                              }
                            }
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          }
        })

      },
    })
  },
  deleteImageClick: function(event) {
    var index = event.currentTarget.dataset.index

    console.log(index)
    this.data.imageArray.splice(index, 1)
    this.setData({
      imageArray: this.data.imageArray
    })

  },
  //3.坐标地址

  locationClick: function(event) {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          lat: res.latitude,
          lng: res.longitude,
          coordinateName: res.name
        })
        console.log(that.data)
      },
    })


  },

  checkImageClick: function(event) {
    var currentUrl = event.currentTarget.dataset.index;
    var list = event.currentTarget.dataset.list;
    console.log(list)
    console.log(currentUrl)
    var tempList = []
    for (var i = 0; i < list.length; i++) {
      var model = list[i]
      tempList.push(model.logoUrl)
    }
    wx.previewImage({
      current: currentUrl,
      urls: tempList,
    })

  },
  submitClick: function(event) {

    var text = this.data.text;

    var picIds = ''
    for (var i = 0; i < this.data.imageArray.length; i++) {
      var imageModel = this.data.imageArray[i]
      if (picIds.length == 0) {
        picIds = imageModel.logoId
      } else {
        picIds = picIds + ',' + imageModel.logoId
      }
    }

    var lat = this.data.lat
    var lng = this.data.lng
    var location = this.data.coordinateName
    if (location == '所在位置') {
      location = ''
    }
   
    console.log(picIds)
    console.log(text)
    console.log(lat)
    console.log(lng)
    console.log(location)
    var url = app.data.httpUrl + 'v1/feed/create'
    var param = {
      userId: app.userData.userId,
      title: text,
      location: location,
      lat: lat,
      lng: lng,
      picIds: picIds,
      isJson: true,
      feedSource: 0
    }
    httpTool.sendRequest(url, param, '发布中...', function(res) {

      wx.showModal({
        title: '温馨提示',
        content: '您的动态已发布成功!',
        success: function(res) {
          app.updateData.isUpdateDynamic = true
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }, function(error) {

    });

  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function() {
    // 生命周期函数--监听页面显示

  },
  onHide: function() {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function() {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数

  },
  onShareAppMessage: function() {
    // 用户点击右上角分享
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  }
})